using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using DAL;
using BUS;
using System.IO;
using Utility;


public partial class Admin_SanPhamAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
        }
    }
    private void LoadDropDownList()
    {
        tbLoaiSP.tbLoaiSPCollection loai_sp_collection = tbLoaiSPBUS.GetAll();
        ddl_loai.DataTextField = tbLoaiSP.tbLoaiSPColumns.TenLoaiSP.ToString();
        ddl_loai.DataValueField = tbLoaiSP.tbLoaiSPColumns.IDLoaiSP.ToString();
        ddl_loai.DataSource = loai_sp_collection ;
        ddl_loai.DataBind();

        tbNhanVien.tbNhanVienCollection  nv_collection = tbNhanVienBUS.GetAll();
        ddl_nhanvien.DataTextField = tbNhanVien.tbNhanVienColumns.TenNV.ToString();
        ddl_nhanvien.DataValueField = tbNhanVien.tbNhanVienColumns.IDNhanVien.ToString();
        ddl_nhanvien.DataSource = nv_collection ;
        ddl_nhanvien.DataBind();

        tbDoanhNghiep.tbDoanhNghiepCollection  dn_collection = tbDoanhNghiepBus .GetAll();
        ddl_doanhnghiep.DataTextField = tbDoanhNghiep.tbDoanhNghiepColumns.TenDoanhNghiep.ToString();
        ddl_doanhnghiep.DataValueField = tbDoanhNghiep.tbDoanhNghiepColumns.IDDoanhNghiep.ToString();
        ddl_doanhnghiep.DataSource = dn_collection ;
        ddl_doanhnghiep.DataBind();

        tbTrangThaiSP.tbTrangThaiSPCollection  ttsp_collection = tbTrangThaiSPBUS.GetAll();
        ddl_tt_sp.DataTextField = tbTrangThaiSP.tbTrangThaiSPColumns.TrangThai.ToString();
        ddl_tt_sp.DataValueField = tbTrangThaiSP.tbTrangThaiSPColumns.IDTrangThaiSP.ToString();
        ddl_tt_sp.DataSource = ttsp_collection;
        ddl_tt_sp.DataBind();

        tbDiaDiem.tbDiaDiemCollection dd_collection = tbDiaDiemBUS.GetAll();
        ddl_diadiem.DataTextField = tbDiaDiem.tbDiaDiemColumns.DiaDiem.ToString();
        ddl_diadiem.DataValueField = tbDiaDiem.tbDiaDiemColumns.IDDiaDiem.ToString();
        ddl_diadiem.DataSource = dd_collection ;
        ddl_diadiem.DataBind();

        tbHangSanXuat.tbHangSanXuatCollection hsx_collection = tbHangSanXuatBUS.GetAll();
        ddl_hsx.DataTextField = tbHangSanXuat.tbHangSanXuatColumns.TenHangSanXuat .ToString();
        ddl_hsx.DataValueField = tbHangSanXuat.tbHangSanXuatColumns.IDHangSanXuat.ToString();
        ddl_hsx.DataSource = hsx_collection;
        ddl_hsx.DataBind();
        //load ngay hientai 
        //txt_ngay_bd.Text = DateTime.Now.ToString("dd/MM/yyyy");
        //txt_ngay_cap_nhat.Text = DateTime.Now.ToString("dd/MM/yyyy");
        txt_ngay_bd.Text = DateTime.Now.ToString("dd/MM/yyyy");
        txt_ngay_bd.ReadOnly = true;
        txt_ngay_cap_nhat.Text = DateTime.Now.ToString("dd/MM/yyyy");

        txt_ngay_bd.Enabled = false;
        txt_ngay_bd.ReadOnly = true;
        txt_ngay_cap_nhat.Enabled = false;
        txt_ngay_cap_nhat.ReadOnly = true;
        


        txt_ngay_cap_nhat.ReadOnly = true;
    }
    protected void bt_them_moi_sp_Click(object sender, EventArgs e)
    {
        tbSanPham sp = new tbSanPham();
        sp.TenSanPham = txt_tensp.Text;
        sp.IDLoaiSP = int.Parse(ddl_loai.SelectedValue);
        sp.IDDiaDiem = int.Parse(ddl_diadiem.SelectedValue);
        sp.IDHangSanXuat = int.Parse(ddl_hsx.SelectedValue);
        sp.IDTrangThaiSP = int.Parse(ddl_tt_sp.SelectedValue);
        sp.IDNhanVien = int.Parse(ddl_nhanvien.SelectedValue);
        sp.IDDoanhNghiep = int.Parse(ddl_doanhnghiep.SelectedValue);
        sp.GiaBan = int.Parse(txt_gia_ban.Text);
        sp.GiaTriThuc = int.Parse(txt_gia_thuc.Text);
        sp.DieuKienSuDung = txt_dk_sudung.Text;
        sp.MoTaSP = txt_mota.Text;
        sp.SoLuongSP = int.Parse(txt_sl_sp.Text);
        sp.SoLuongBan = int.Parse(txt_sl_ban.Text);
        sp.SoLuongDaBan = int.Parse(txt_sl_daban.Text);
        sp.NgayBDBan = DateTime.Parse(txt_ngay_bd.Text);
        sp.NgayKTBan = DateTime.Parse(txt_ngay_kt.Text);
        sp.NgayCapNhat = DateTime.Parse(txt_ngay_cap_nhat.Text);
        sp.TiLeHoaHong = int.Parse(txt_tylehoahong.Text);


       int idsp= tbSanPhamBus.Insert_SanPham(sp);

        HttpFileCollection hfc = Request.Files;
        for (int i = 0; i < hfc.Count; i++)
        {
            HttpPostedFile hpf = hfc[i];
            if (hpf.ContentLength > 0)
            {

                string filename = DateTime.Now.Ticks.ToString() + "_" + Path.GetFileName(hpf.FileName);
                hpf.SaveAs(Server.MapPath("~/Admin/Photos/" + filename));

                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Admin/Photos/" + filename));

                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/Admin/Photos/Thumb/"), filename);
                tbHinhAnh ha = new tbHinhAnh();
                ha.IDSanPham = idsp;
                ha.TenHinh = filename;
                ha.DuongDan = filename;

                tbHinhAnhBus.Insert_HinhAnh(ha);
            }
        }


        Response.Redirect("SanPhamList.aspx");
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        DateTime datetime = Calendar1.SelectedDate;
        this.txt_ngay_kt.Text = datetime.Day + "/" + datetime.Month + "/" + datetime.Year;
        this.Calendar1.Visible = false; 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.Calendar1.Visible = true ;
    }
}
